#include <string>
#include <fstream>
#include <iostream>
#include <vector>
#include <sstream>
#include "api.h"


void writeTuple(Record* rec, std::stringstream& _testResultStream)
{
	if(rec==0){
		_testResultStream<<"\nnie znaleziono"<< std::endl;
	}else{
		_testResultStream<<"\n***START KROTKA***"<< std::endl;

		_testResultStream <<"\tinty: ";
	  	for(int i=0;i<rec->ints.size();++i)
	  	{
	  		_testResultStream <<rec->ints.at(i)<<"*";
	  	}
	  	_testResultStream << std::endl;

	  	_testResultStream <<"\tfloaty: ";
	  	for(int i=0;i<rec->floats.size();++i)
	  	{
	  		_testResultStream <<rec->floats.at(i)<<"*";
	  	}
	  	_testResultStream << std::endl;

	  	_testResultStream <<"\tstringi: ";
		for(int i=0;i<rec->strings.size();++i)
		{
			_testResultStream <<rec->strings.at(i)<<"*";
		}
		_testResultStream << std::endl;
		_testResultStream <<"***KONIEC KROTKI***\n"<<endl;
	}
}






int main(int argc, char **argv)
{
	std::string 	procededLine;
	std::vector<string>	testLines;
	std::vector<string>	operation;
	std::vector<string> lindaQuery;
	std::ifstream			testFile;
	std::ofstream			testResult;

	if(argc < 3)
	{
		std::cout << "Podaj argumenty wej/wyj! Wychodze...\n";
		return -1;
	}


	testFile.open(argv[1]);


	if(!testFile.is_open())
	{
		std::cout << "Nie udało się otowrzyć pliku z testem. Wychodze...\n";
		return -1;
	}




	while(! testFile.eof() )
	{
		getline (testFile,procededLine);
		testLines.push_back(procededLine);
	}



	std::vector<string>::iterator lineIt = testLines.begin();


	for(; lineIt != testLines.end() ; ++lineIt)
	{
		std::vector<string> tokens;
		Tokenize(*lineIt, tokens, "#");
		if(tokens.size() != 2)
			continue;
		operation.push_back(tokens[0]);
		lindaQuery.push_back(tokens[1]);
	}

	testFile.close();


	ApiLinda LINDA("nie warto tego robic",100);

	int limit = operation.size();

	std::vector<string> outPutToFile;
	Record* rec;

	for(int i = 0 ; i < limit ; ++i)
	{
		std::stringstream testOutput;



		if(operation[i] == "read")
		{
			rec = LINDA.readLinda(lindaQuery[i]);
			testOutput << i <<". read";
			writeTuple(rec, testOutput);
			outPutToFile.push_back(testOutput.str());
		}



		else if(operation[i] == "input")
		{
			rec  = LINDA.inputLinda(lindaQuery[i]);
			testOutput << i <<". input";
			writeTuple(rec, testOutput);
			outPutToFile.push_back(testOutput.str());
		}



		else if(operation[i] == "output")
		{
			testOutput << "\n" << i << ". Zapis\n";
			testOutput << "Krotka: "  << lindaQuery[i] << "\n";
			LINDA.outputLinda(lindaQuery[i]);
			testOutput << "Koniec zapisu\n";
			outPutToFile.push_back(testOutput.str());
		}



		else
		{
			std::cout << "Zly format testu nr: " << i+1 << std::endl;
			continue;
		}

	}





	testResult.open(argv[2]);
	if(!testResult.is_open())
	{
		std::cout << "Nie udało się otowrzyć pliku do zapisu wynikow. Wychodze...\n";
		return -1;
	}

	for(int i=0 ; i < outPutToFile.size() ; i++)
	{
		testResult << outPutToFile[i] <<  endl;
	}

	testResult.close();


	//LINDA.pisz();



	//LINDA.pisz();
	///LINDA.outputLinda("string:olakabana,float:3.14,integer:555,float:2.71,string:kurde");
	//Record* rec = LINDA.inputLinda("integer:==555");
//DEBUG("MAIN", (int)rec);



	///WAZNY KOD NIE ZMENIAĆ
/*

  testFile.open(argv[1]);
	testResult.open(argv[2]);
	if(!testFile.is_open())
	{
		std::cout << "Nie udało się otowrzyć pliku z testem. Wychodze...\n";
		return -1;
	}

	if(!testResult.is_open())
	{
		std::cout << "Nie udało się otowrzyć pliku do zapisu wynikow. Wychodze...\n";
		return -1;
	}

	
	while(! testFile.eof() )
	{
		getline (testFile,procededLine);
		testLines.push_back(procededLine);
	}

	std::vector<string>::iterator lineIt = testLines.begin();


	for(; lineIt != testLines.end() ; ++lineIt)
	{
		std::vector<string> tokens;
		Tokenize(*lineIt, tokens, "#");
		if(tokens.size() != 2)
			continue;
		operation.push_back(tokens[0]);
		lindaQuery.push_back(tokens[1]);
	}


	int limit = operation.size();

	for(int i = 0 ; i < limit ; ++i)
	{
		if(operation[i] == "read")
		{
			LINDA.readLinda(lindaQuery[i]);
		}
		else if(operation[i] == "input")
		{
			LINDA.inputLinda(lindaQuery[i]);
		}
		else if(operation[i] == "output")
		{
			LINDA.outputLinda(lindaQuery[i]);
		}

		else
		{
			std::cout << "Zly format testu nr: " << i+1 << std::endl;
			continue;
		}

	}
	testFile.close();
	testResult.close();
	*/

}


